A view is a database object that represents a saved SELECT
statement and is also referred to as a virtual or logical table. Views
can be queried in the same way as tables, and some types of views can
be updated, too. Using views instead of tables can greatly simplify
data access and decouple client applications from the underlying tables
containing actual data. With the appropriate use of views, it is
possible to completely change the schema of the database and redesign
the tables without breaking any client applications. Think of views as
an abstract interface between your physical database tables and the
people or applications querying them.
Creating Views
SQL Server 2008 allows you to create views of the following types:
Standard view This view is based on one or more base tables. The view may include joins, filter restrictions (using the WHERE clause), and row count restrictions (using the TOP and ORDER BY clauses). You cannot use the ORDER BY clause in a view without specifying the TOP clause as well.
Updateable view A view that is based on a single underlying table can be updated directly. Executing INSERT, UPDATE, DELETE, and MERGE statements on this type of view will affect the data in the underlying table. You can also define an INSTEAD OF INSERT, INSTEAD OF UPDATE, and INSTEAD OF DELETE trigger on any view, which will perform a particular action when you attempt to insert, update, or delete data in the view.
Indexed view
Sometimes it is valuable to create one or more indexes on a view in
order to optimize the time it takes to query the view. Indexes can be
created on views using standard CREATE INDEX syntax. Indexed views must be created with the SCHEMABINDING option (see the “Using the SCHEMABINDING Option to Lock in a View’s Underlying Schema” sidebar).
Partitioned view
A partitioned view joins data that is spread across a table partitioned
horizontally—for example, if you have partitioned a table by OrderDate
to store orders from five years ago and earlier in one partition,
orders created within the last five years in another partition, and
orders created this year in yet another partition. A partitioned view
will join all the partitions together into one Orders virtual table
containing data from all three partitions.
To create a view, use the CREATE VIEW statement syntax shown in Example 1.
Example 1. CREATE VIEW Statement—Syntax
CREATE VIEW [schema_name].view_name [(column_names)] [ WITH ENCRYPTION | SCHEMABINDING ] AS select_statement [ WITH CHECK OPTION ]
|
Specifying the column_names in a view definition allows you to assign names to computed columns or to rename columns produced by the SELECT
statement. This is useful for calculated columns and columns that may
have ambiguous names. If you don’t specify explicit column names, the
view columns will inherit the same names as the columns in the SELECT statement.
Specifying the WITH ENCRYPTION option encrypts the view definition. This also prevents the view from being used in replication.
Views are named SELECT
statements and include one or more columns from one or more tables.
What will happen if a column or table referenced by a view is dropped
from the database? The view will become invalid and will return an
error the next time it is queried. To lock the view into the schema
objects on which it relies, add the WITH SCHEMABINDING option to your CREATE VIEW statement.
This
option ensures that any table or column referenced by this view cannot
be dropped or altered, until the view itself is dropped. This applies
only to columns referenced by the view. You can freely add and remove
columns from underlying tables, as long as they are not used in the
view.
Only specify the SCHEMABINDING option when the view references tables from a single database. You must specify the SCHEMABINDING option if you wish to build indexes on the view you are creating.
|
Example 2 creates a view based on the Stars table using the SCHEMABINDING option. We then attempt to alter the underlying structure of the base table but receive an error. Figure 1 demonstrates how the same view can be created using the graphical view designer in SQL Server Management Studio.
Example 2. Working with Views
CREATE VIEW MyStarsView WITH SCHEMABINDING AS SELECT StarName, StarType FROM dbo.Stars WHERE SolarMass >=1; GO SELECT * FROM MyStarsView; -- Results: -- StarName StarType -- ---------- -------------------------------------------------- -- Deneb White supergiant -- Pollux Orange Giant -- Sun Yellow dwarf ALTER TABLE Stars DROP COLUMN StarType; GO -- Results: --Msg 5074, Level 16, State 1, Line 1 -- The object 'MyStarsView' is dependent on column 'StarType'. -- Msg 5074, Level 16, State 1, Line 1 -- ALTER TABLE DROP COLUMN StarType failed because one or more objects access this column. -- This view is updateable, as it is based upon only one base table UPDATE MyStarsView SET StarType = 'White Supermassive Giant' WHERE StarType = 'White supergiant' GO SELECT * FROM MyStarsView; -- Results: -- StarName StarType -- ---------- -------------------------------------------------- -- Deneb White Supermassive Giant -- Pollux Orange Giant -- Sun Yellow dwarf
|